#!/usr/bin/ebv/ python3
# -*- coding: utf-8 -*-
#-*- encoding=utf-8

# 因为我的电脑有两个版本的python 所以需要用pip3 install pillow命令按照
#第三方库

#1.)pillow库,处理图像 文档地址:https://pillow.readthedocs.org/ 
from PIL import Image,ImageFilter,ImageDraw,ImageFont
import os,random

scriptUrl = 'D:/workspace/python/learnpy/'
imgurl = os.path.join(scriptUrl,'img')
imgurl = os.path.join(imgurl,'flower.jpg')

def getSrcImg():
	img = Image.open(imgurl)
	return img
def getDir(dirname):
	ret = os.path.join(scriptUrl,dirname)
	return ret 
def joinUrl(dirname,urlname):
	ret = os.path.join(dirname,urlname)
	return ret 
	
def getImgUrl(imgName):
	ret = os.path.join(scriptUrl,imgName)
	return ret

# 打开一个jpg图像文件，注意是当前路径:
img = getSrcImg()
print('img=',img,img.size) #图片尺寸
w,h=img.size
#设置缩略图; 参数中 550/100=w/h 按照这样的等比缩放设置缩略图的尺寸
img.thumbnail((550,100)); #不改变原始图像,copy一个

img.save(os.path.join(scriptUrl,'simg.jpg'),'jpeg')
print('img.size=',img.size)

#img = getSrcImg()
#img2 = img.filter(ImageFilter.GaussianBlur(20))
#img2.save(getImgUrl('gasBlur.jpg'),'jpeg')

#print(random.randint(9,11)) #随机数字 9-11 包括边界值

#生成验证码的类
class VerifyCode(object):
	
	def __init__(self,w,h,num,fontSize,*space):
		self._width = w;
		self._height = h;
		self._num = num;
		self._totalW = self._width*self._num;
		self._fontSize = fontSize;
		self._codeStr="";
		if space:
			self._space = space[0];
		else:
			self._space = {'x':10,'y':10};
		
	def draw(self):
		self._img = Image.new('RGB',(self._totalW,self._height),(255,255,255));#创建一个白色的画布		
		self._font = self.getFont();
		self._drawObj = ImageDraw.Draw(self._img)
		self.fillDraw()
		self.fillText();
		
	def fillText(self):
		for i in range(self._num):
			#第一参数为坐标,第二个是要画的字符
			self._drawObj.text((self._width*i+self._space['x'],self._space['y']),self.makeCode(),font=self._font,fill=self.rndColor2());
	
	#随机填充画布
	def fillDraw(self):		
		for x in range(self._totalW):
			for y in range(self._height):
				self._drawObj.point((x,y),fill=self.rndColor()) #随机填充每个像素的颜色
	
	#生成验证码
	def makeCode(self):
		idx = random.randint(1,2);
		code = 0;
		if idx==1:
			code = self.randChar();
		else:
			code = str(self.randomNum());
		self._codeStr+=str(code);
		return code
	
	def getFont(self,fontname='arialbd.ttf'):
		global scriptUrl		
		fonturl = os.path.join(scriptUrl,'font')
		fonturl = os.path.join(fonturl,fontname)
		font = ImageFont.truetype(fonturl, self._fontSize);
		return font;
	
	
	def randChar(self):
		return chr(random.randint(65,90)); #大写字母
		
	def randomNum(self):
		return random.randint(0,9);
		
	# 随机颜色1:背景颜色(相对文字偏亮)
	def rndColor(self):
		return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
		
	# 随机颜色2:文字颜色(相对背景偏暗)
	def rndColor2(self):
		return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
	
	def save(self,url):
		self._img.save(url,'jpeg')
		print('codeStr=',self._codeStr,'self._codeStr lower=',self._codeStr.upper(),'url=',url)
	
	#验证码是否正确
	def verify(code):
		return self._codeStr.lower()==code.lower();



for i in range(5):
	space = {'x':random.randint(5,15),'y':random.randint(5,12)}	
	codeImg = VerifyCode(50,50,4,25,space)
	codeImg.draw()
	imgurl = getDir('img')
	imgurl = joinUrl(imgurl,'code_'+str(i+1)+'.jpg')	
	codeImg.save(imgurl)







